package com.ch.livedatademo

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders

class FirstFragment : Fragment() {

    private lateinit var mView: View;
    private lateinit var mSeekBar: SeekBar
    private lateinit var myViewModel: MyViewModel
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mView = inflater.inflate(R.layout.first_layout, container, false)
        mSeekBar = mView.findViewById(R.id.first_seek_bar)
        myViewModel = activity?.let { ViewModelProviders.of(it).get(MyViewModel::class.java) }!!
        myViewModel.getProgress().observe(requireActivity()) {
            mSeekBar.progress = it
        }
        mSeekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                progress?.let {
                    myViewModel.getProgress().value=it
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }

        })
        return mView
    }

}